{ "cells": [ { "cell_type": "markdown", "id": "1d42b084-5ed8-47b1-b052-8fd50b335582", "metadata": {}, "source": [ "# Performance of Matrix Computation\n", "**강좌**: *수치해석*" ] }, { "cell_type": "markdown", "id": "37fe0de3-1ca8-4a35-afcc-720d99beb9f7", "metadata": {}, "source": [ "# Matrix Computation\n", "- 많은 수치 계산은 Matrix 또는 Vector 연산임\n", "- 다양한 선형대수 수치 라이브러리가 존재함\n", " * BLAS\n", " * LAPACK, LINPACK\n", " * PETSc 등\n", "\n", "## BLAS Library\n", "- 기본 행렬/벡터 연산 라이브러리\n", "- 벡터-벡터 (Level 1), 행렬-벡터 (Level 2), 행렬-행렬 연산 (Level 3)\n", "- Intel MKL, NVIDIA CuBLAS 등 하드웨어 제조사에서 최적화된 라이브러리 제공\n", " * OpenBLAS 등의 라이브러리도 존재함\n", "- Numpy도 BLAS, LAPACK 등을 활용하여 array 연산 수행함\n", "\n", "## FLOPS\n", "- Floationg Point Operations per Second\n", "- 초당 부동소숫점 연산 속도\n", "- 컴퓨터의 주요 성능 지표 중 하나임\n", " * [Top 500 Lists](https://top500.org/)\n", "- 유일한 성능 지표는 절대 아님!!!\n", " \n", "## GEMM 연산 속도 측정\n", "- GEMM (General Matrix to Matrix Multiplication)\n", " * 대표적인 연산 속도 측정 방법\n", "\n", "- `%timeit` 함수를 이용해서 $m=l=n=4096$ 인 경우 평균 연산 시간과 FLOPS를 측정하라\n", " * Double precision 과 Single Precision 모두 측정하라\n", " * 사용중인 CPU의 이론 성능과 비교해보자\n", " - https://en.wikichip.org/wiki/flops\n", "- Example\n", " * [i7 1360p processor](https://www.techpowerup.com/cpu-specs/core-i7-1360p.c3059)\n", " * Theoetrical peak: 16 DP FLOPS/cylce * 4 cores (P) * 5.0Ghz = 320 GFLOPs" ] }, { "cell_type": "code", "execution_count": 1, "id": "f31464bb-db01-45e8-8cca-5375878f4edc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "690 ms ± 17.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", "Measured FLOPS : 199.2197 GFLOPS\n" ] } ], "source": [ "import numpy as np\n", "\n", "\n", "m=n=l=4096\n", "\n", "a = np.random.rand(m, n)\n", "b = np.random.rand(n, l)\n", "c = np.random.rand(m, l)\n", "\n", "t = %timeit -o c[:] = a @ b\n", "\n", "flops = 2*m*n*l / t.average\n", "print(\"Measured FLOPS : {:.4f} GFLOPS\".format(flops*1e-9))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.2" } }, "nbformat": 4, "nbformat_minor": 5 }